package com.xingshang.controller;


import com.xingshang.model.dto.GoodsDto;
import com.xingshang.model.po.Category;
import com.xingshang.model.po.User;
import com.xingshang.service.CategoryService;
import com.xingshang.service.GoodsService;
import com.xingshang.service.UserService;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/admin")
@RequiredArgsConstructor
public class AdminController {

    private final UserService userService;
    private final CategoryService categoryService;
    private final GoodsService goodsService;

    @GetMapping("")
    public String admin(Model model, HttpSession session) {
        System.out.println("管理员页面");
        User user = (User) session.getAttribute("user");
        if(user == null||!user.getAccount().equals("admin")){
            return "redirect:/user/login";
        }
//        model.addAttribute("site", "星殇商城");
        return "admin";
    }

    @GetMapping("/goods/add")
    public String goodsAdd(Model model, HttpSession session) {
        System.out.println("添加商品页面");
//        model.addAttribute("site", "星殇商城");
        List<Category> list = categoryService.list();
        model.addAttribute("categories", list);
        return "goods_add";
    }

    @PostMapping("/goods/add")
    public String addGoods(GoodsDto goodsDto) {
        System.out.println(goodsDto);
        boolean b = goodsService.insertGood(goodsDto);
        return null;
    }

    @PostMapping("/upload/good")
    @ResponseBody
    public String uploadGoodPhotos(MultipartFile file) throws Exception {
        String s = goodsService.addMediaFilesToMinIO(file);
        return s;
    }
}
